package com.exampl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exampl.common.R;
import com.exampl.domain.OrderDetail;
import com.exampl.domain.Orders;
import com.exampl.domain.ShoppingCart;
import com.exampl.domain.User;
import com.exampl.dto.OrdersDto;
import com.exampl.service.OrderDetailService;
import com.exampl.service.OrdersService;
import com.exampl.service.ShoppingService;
import com.exampl.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrdersService ordersService;

    @Autowired
    UserService userService;

    @Autowired
    OrderDetailService orderDetailService;

    @Autowired
    ShoppingService shoppingService;

    // 提交结算
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("提交成功！");
    }

    // 用户查看订单
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> pageR(Integer page, Integer pageSize){
        Page<OrdersDto> dtoPage = ordersService.getPage(page, pageSize);
        return R.success(dtoPage);
    }

    // 后台查看订单
    @GetMapping("/page")
    public R<Page<OrdersDto>> orderPage(Integer page, Integer pageSize, String number, String beginTime, String endTime, HttpSession session){
        Page<Orders> page1 = new Page<Orders>(page, pageSize);
        Page<OrdersDto> page2 = new Page<OrdersDto>();
        // 设置条件查询
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<Orders>();
        lqw.like(number != null, Orders::getNumber, number);
        lqw.between(beginTime != null && endTime != null, Orders::getCheckoutTime, beginTime, endTime);
        lqw.orderByDesc(Orders::getOrderTime);
        ordersService.page(page1, lqw);
        BeanUtils.copyProperties(page1, page2, "records");
        List<Orders> orders = page1.getRecords();
        // 设置用户名
        List<OrdersDto> dtoList = orders.stream().map((data) -> {
            OrdersDto dto = new OrdersDto();
            // 获取用户名
            Long userId = data.getUserId();
            User user = userService.getById(userId);
            String name = user.getName();
            BeanUtils.copyProperties(data, dto);
            dto.setUserName(name);
            return dto;
        }).collect(Collectors.toList());
        page2.setRecords(dtoList);
        return R.success(page2);
    }

    // 后台派送与完成订单
    @PutMapping
    public R<String> give(@RequestBody Orders orders){
        ordersService.updateById(orders);
        return R.success("修改状态成功！");
    }

    // 再来一单
    @PostMapping("/again")
    public R<String> again( HttpSession session, @RequestBody Orders orders){
        // 获取用户id
        Long user_id = (Long) session.getAttribute("user");
        Long id = orders.getId();
        LambdaQueryWrapper<OrderDetail> lqw = new LambdaQueryWrapper<OrderDetail>();
        lqw.eq(OrderDetail::getOrderId, id);
        List<OrderDetail> details = orderDetailService.list(lqw);
        for(OrderDetail detail:details){
            ShoppingCart cart = new ShoppingCart();
            BeanUtils.copyProperties(detail, cart, "id");
            cart.setUserId(user_id);
            shoppingService.save(cart);
        }
        return R.success("下单成功！");
    }

    // 取消订单
    @PostMapping("/cancel")
    public R<String> cancel(@RequestBody Orders orders){
        orders.setStatus(5);
        ordersService.updateById(orders);
        return R.success("取消成功！");
    }
}
